var prefix = jsctx + "/tenant/serviceType";

var openService = (function () {

    function open(code) {
        // layer.confirm('确定要申请开通？', {
        //     btn: ['确定', '取消']
        // }, function (index) {
        //     $.ajax({
        //         url: prefix + "/toOpenService",
        //         type: "post",
        //         data: {
        //             'serviceTypeCode': code
        //         },
        //         success: function (data) {
        //             layer.close(index);
        //             if (data.status == 200) {
        //                 layer.msg(data.message);
        //                 location.href=prefix+"/tenant/serviceType/tenantIndex/"+code
        //             } else {
        //                 layer.msg(data.message);
        //             }
        //         }
        //     });
        // })
        layer.open({
            type: 1,
            title: '开通提示',
            closeBtn: false,
            shade: 0.4,
            shift: 7,
            shadeClose: true,
            content: `<div style='width:350px;padding:10px;text-align: left'>
                        
                        <div >
                        <span style="font-size: 16px">请输入购买年限:</span>
                        <input type="text" id="effectivePeriod" onkeyup="value=value.replace(/^(0+)|[^\\d]+/g,'')" style="width: 100px;height: 30px;margin-left: 10px;text-align: center"  value="1"><span style="margin-left: 5px">年</span>
                        </div>
                        <!--<div style="margin-top: 10px">-->
                        <!--<span style="font-size: 16px">请选择购买数量:</span>-->
                        <!--<input type="text" id="amount" onkeyup="value=value.replace(/^(0+)|[^\\d]+/g,'')" style="width: 100px;height: 30px;margin-left: 10px;text-align: center"  value="1"><span style="margin-left: 5px">张</span>-->
                        <!--</div>-->
                        <!--<button style='margin-top:80px;' type='button' class='btn btn-block btn-success btn-lg' >提交</button>-->
                       </div>`,
            btn: ['确定', '取消'],
            yes: function (index) {//layer.msg('yes');    //点击确定回调
                if (!$("#effectivePeriod").val()) {
                    layer.msg("请输入购买年限");
                    return;
                }
                // if(!$("#amount").val()){
                //     layer.msg("请输入购买数量");
                //     return;
                // }
                layer.close(index);
                $.ajax({
                    url: prefix + "/toOpenService",
                    type: "post",
                    data: {
                        'serviceTypeCode': code,
                        // 'amount':$("#amount").val(),
                        'effectivePeriod': $("#effectivePeriod").val()
                    },
                    success: function (data) {
                        console.log(data)
                        layer.close(index);
                        if (data.status == 200) {
                            layer.msg(data.message);
                            location.href = prefix + "/tenantIndex/" + code
                        } else {
                            layer.msg(data.message);
                        }
                    }
                });
            },
        });

    }


    return {
        open: open,
        // reLoad:reLoad
    };
})();


var updateOpenService = (function () {
    function init() {
        validateRule();
    };

    function getCheckedRoles() {
        var adIds = "";
        $("input:checkbox[name=role]:checked").each(function (i) {
            if (0 == i) {
                adIds = $(this).val();
            } else {
                adIds += ("," + $(this).val());
            }
        });
        return adIds;
    }

    var isajaxsend = true;

    function save() {
        if (isajaxsend) {
            var formData = new FormData($("#frmOpenService")[0]);
            $("#roleIds").val(getCheckedRoles());
            $.ajax({
                cache: true,
                type: "POST",
                url: prefix + "/updateOpenService",
                data: formData,
                processData: false,
                contentType: false,
                async: false,
                beforeSend: function () {
                    isajaxsend = false
                },
                error: function (request) {
                    parent.layer.alert("Connection error");
                },
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        $('#exampleTable').bootstrapTable('refresh');
                    } else {
                        parent.layer.alert(data.message)
                    }

                },
                complete: function () {
                    isajaxsend = false
                }
            });
        }
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmOpenService").validate({
            rules: {},
            messages: {},
            submitHandler: function (form) {
                save();
            }
        })
    }

    return {
        init: init
    };
})();



